package com.ruoyi.project.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.ImgUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.domain.MonthlyImageDetail;
import com.ruoyi.project.mapper.MonthlyImageDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.MonthlyImageMapper;
import com.ruoyi.project.domain.MonthlyImage;
import com.ruoyi.project.service.IMonthlyImageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 月度形象图Service业务层处理
 * 
 * @author liboyu
 * @date 2024-06-04
 */
@Service
public class MonthlyImageServiceImpl implements IMonthlyImageService 
{
    @Autowired
    private MonthlyImageMapper monthlyImageMapper;
    @Autowired
    private MonthlyImageDetailMapper monthlyImageDetailMapperMapper;

    /**
     * 查询月度形象图
     * 
     * @param imageId 月度形象图主键
     * @return 月度形象图
     */
    @Override
    public MonthlyImage selectMonthlyImageByImageId(Long imageId)
    {
        return monthlyImageMapper.selectMonthlyImageByImageId(imageId);
    }

    /**
     * 查询月度形象图列表
     * 
     * @param monthlyImage 月度形象图
     * @return 月度形象图
     */
    @Override
    public List<MonthlyImage> selectMonthlyImageList(MonthlyImage monthlyImage)
    {
        // 查询月度形象图
        List<MonthlyImage> imageList = monthlyImageMapper.selectMonthlyImageList(monthlyImage);

        List<MonthlyImage> collect = imageList.stream().peek(image -> {

            if (StringUtils.isEmpty(image.getDetailString())){
                return;
            }

            //把详情图信息按,拆成listString
            List<String> detailStringList = Arrays.stream(image.getDetailString().split(",")).collect(Collectors.toList());

            //遍历string,按@拆成数组,赋值到MonthlyImageDetail,添加到list,然后在添加到MonthlyImage里
            List<MonthlyImageDetail> details = new ArrayList<>();
            detailStringList.forEach(detailStringArrayString -> {

                String[] detailStringArray = detailStringArrayString.split("@");
                MonthlyImageDetail monthlyImageDetail = new MonthlyImageDetail();
                monthlyImageDetail.setImageDetailId(Long.parseLong(detailStringArray[0]));
                monthlyImageDetail.setImageId(Long.parseLong(detailStringArray[1]));
                monthlyImageDetail.setImageDetailPic(detailStringArray[2]);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                try {
                    date = sdf.parse(detailStringArray[3]);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                monthlyImageDetail.setImageDetailDate(date);
                // 放进详情图实体类的list
                details.add(monthlyImageDetail);
            });
            // 按日期排序
            details.sort(Comparator.comparing(MonthlyImageDetail::getImageDetailDate));
            // 把详情图实体类的list赋值到MonthlyImage实体类
            image.setDetails(details);
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 新增月度形象图
     * 
     * @param monthlyImage 月度形象图
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor ={Exception.class, Error.class} )
    public int insertMonthlyImage(MonthlyImage monthlyImage)
    {
        // 设定时间为当前时间
        monthlyImage.setUpdateDate(new Date());
        return monthlyImageMapper.insertMonthlyImage(monthlyImage);
    }

    /**
     * 修改月度形象图
     * 
     * @param monthlyImage 月度形象图
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor ={Exception.class, Error.class} )
    public int updateMonthlyImage(MonthlyImage monthlyImage)
    {
        // 设定时间为当前时间
        monthlyImage.setUpdateDate(new Date());


        // 查询数据库中图片的路径
        String imagePathInDb = monthlyImageMapper.selectMonthlyImageByImageId(monthlyImage.getImageId()).getEffectSrc();
        // 获取前端传过来的图片路径
        String imageFromView = monthlyImage.getEffectSrc();


        //执行修改
        int i = monthlyImageMapper.updateMonthlyImage(monthlyImage);

        // 删除垃圾图片
        ImgUtil.delTrushImg(i, imagePathInDb, imageFromView);

        return i;
    }



    /**
     * 批量删除月度形象图
     * 
     * @param imageIds 需要删除的月度形象图主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor ={Exception.class, Error.class} )
    public int deleteMonthlyImageByImageIds(Long[] imageIds)
    {
        // 查询数据库里图片的路径
        String imagePathInDb = monthlyImageMapper.selectMonthlyImageByImageId(imageIds[0]).getEffectSrc();


        // 查询所有详情的数据库中的数据

        MonthlyImageDetail monthlyImageDetail = new MonthlyImageDetail();
        monthlyImageDetail.setImageId(imageIds[0]);
        List<MonthlyImageDetail> monthlyImageDetails = monthlyImageDetailMapperMapper.selectMonthlyImageDetailList(monthlyImageDetail);

        // 删除所有月度图下的详情
        int iDetail = monthlyImageDetailMapperMapper.deleteMonthlyImageDetailByImageId(imageIds[0]);

        //没成功直接返回0
        if (iDetail == 0) {
            return 0;
        }
        // 遍历数据集合并删除图片
        monthlyImageDetails.forEach(detail -> {
            // 删除数据库里的图片
            ImgUtil.delDbImg(detail.getImageDetailPic());
        });

        //删除主题数据
        int iMonth = monthlyImageMapper.deleteMonthlyImageByImageIds(imageIds);

        //没成功直接返回0
        if (iMonth == 0) {
            return 0;
        }

        //删除数据库里的主题图片
        ImgUtil.delDbImg(imagePathInDb);
        return iMonth;
    }

    /**
     * 删除月度形象图信息
     * 
     * @param imageId 月度形象图主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor ={Exception.class, Error.class} )
    public int deleteMonthlyImageByImageId(Long imageId)
    {


        // 查询数据库里图片的路径
        String imagePathInDb = monthlyImageMapper.selectMonthlyImageByImageId(imageId).getEffectSrc();
        //删除数据库里的图片
        ImgUtil.delDbImg(imagePathInDb);

        // 查询所有详情的数据库中的数据

        MonthlyImageDetail monthlyImageDetail = new MonthlyImageDetail();
        monthlyImageDetail.setImageId(imageId);
        List<MonthlyImageDetail> monthlyImageDetails = monthlyImageDetailMapperMapper.selectMonthlyImageDetailList(monthlyImageDetail);

        // 遍历数据集合并删除图片
        monthlyImageDetails.forEach(detail -> {
            // 删除数据库里的图片
            ImgUtil.delDbImg(detail.getImageDetailPic());
        });


        // 删除所有月度图下的详情
        int i = monthlyImageDetailMapperMapper.deleteMonthlyImageDetailByImageId(imageId);
        return monthlyImageMapper.deleteMonthlyImageByImageId(imageId);
    }

    @Override
    public Long checkNameUnique(MonthlyImage monthlyImage) {
        return monthlyImageMapper.checkNameUnique(monthlyImage);
    }
}
